package com.designoutput.ui.options
{
	import com.designoutput.elements.LineElement;
	import com.designoutput.enum.ToolType;
	import com.designoutput.event.ColorPadEvent;
	import com.designoutput.event.SelectEvent;
	import com.designoutput.event.SlideControlEvent;
	import com.designoutput.icon.PencilIcon;
	import com.designoutput.ui.controls.Checkbox;
	import com.designoutput.ui.controls.ColorPad;
	import com.designoutput.ui.controls.Dropdown;
	import com.designoutput.ui.controls.Dropslider;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.controls.ToolButton;
	import com.designoutput.ui.util.ColorPicker;
	import com.designoutput.utils.Unit;
	import com.designoutput.utils.UnitUtils;
	
	import flash.display.Sprite;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import com.designoutput.manager.ResourceManager;

	public class LineOption extends BaseOption
	{
		
		public var lineGroup:Sprite;
		public var useLineChx:Checkbox;
		public var lineFlag:ToolButton;
		public var lineColorPad:ColorPad;
		public var lineSizeDrp:Dropdown;
		public var lineAlpha:Dropslider;
		
		public function LineOption(parent:DesignoutputCard)
		{
			super(parent);
			
			/**********************************************/
			lineGroup = new Sprite();
			lineGroup.x = 0;
			
			lineColorPad = new ColorPad(0,true);
			lineColorPad.addEventListener(MouseEvent.MOUSE_DOWN, this.lineColorPadMouseDown);
			lineColorPad.addEventListener(ColorPadEvent.CHOOSE_COLOR, this.lineColorChange);
			lineColorPad.x = 0;
			lineColorPad.y = 3;
			lineGroup.addChild(lineColorPad);
			
			lineSizeDrp = new Dropdown(45,45);
			lineSizeDrp.addEventListener(SelectEvent.COMMAND, lineSizeChange);
			lineSizeDrp.x = 35;
			lineSizeDrp.y = 5;
			
			/*******************/
			var lis:Literal = new Literal(ResourceManager.getValue("rm_line_alpha"));
			lis.x = 0;
			lis.y = 30;
			lineGroup.addChild(lis);
			
			lineAlpha = new Dropslider(45,0,100,100,100);
			lineAlpha.addEventListener(SlideControlEvent.DRAG, this.lineAlphaChange);
			lineAlpha.x = 75;
			lineAlpha.y = 30;
			lineGroup.addChild(lineAlpha);
			lineGroup.addChild(lineSizeDrp);
			
			/**********************************************/
			addChild(depthManager);
			addChild(lineGroup);
			
			initLineSize();
			
			resize();
		}
		
		private function setDefaultInforTitle():void{
			this.elementBaseInfor.setPositionTitle(ResourceManager.getValue("rm_base_start_line_title"));
			this.elementBaseInfor.setSizeTitle(ResourceManager.getValue("rm_base_end_line_title"));
		}
			
		
		public function resize():void{
			lineGroup.y = 117;	
			depthManager.y = 177;	
		}
		
		private function initLineSize():void{
			var lineSizeArr:Array = new Array(1, 2, 3, 4, 5, 6, 8, 10, 12, 16,20, 25, 30, 40, 50, 60, 80, 100);
			var len:int = lineSizeArr.length;
			for(var i:int = 0;i<len;i++){
				lineSizeDrp.addItem(lineSizeArr[i].toString(),lineSizeArr[i].toString(), lineSizeArr[i]=="1");
			}
			lineSizeDrp.bind();
		}
		
		private function lineColorPadMouseDown(evt:MouseEvent):void{
			new ColorPicker(this.designOutputCard, evt.target as ColorPad).show(true, true);
		}
		
		/**
		 * Show line element's information (x1,y1,x2,y2) 
		 * @param element
		 * @param bothShirtAndSleeves
		 * @param forSleeveElements
		 * 
		 */		
		public function updateOptions(element:LineElement,bothShirtAndSleeves:Boolean=false,forSleeveElements:Boolean=false):void{
			setDefaultInforTitle();
			
			this.elementBaseInfor.setBaseInfor(
				UnitUtils.convert(element.x, Unit.PIXELS,  card.unit),
				UnitUtils.convert(element.y, Unit.PIXELS,  card.unit),
				UnitUtils.convert(element.x+element.width, Unit.PIXELS,  card.unit),
				UnitUtils.convert(element.y+element.height, Unit.PIXELS,  card.unit),
				element.skewX,
				element.skewY
			);
			
			this.lineSizeDrp.Set(element.lineSize.toString());
			this.lineColorPad.Set(element.lineColor);
			this.lineAlpha.setValue(element.lineAlpha*100);
		}
		
		private function lineColorChange(evt:ColorPadEvent):void{
			var lineColor:Number=new Number(this.lineColorPad.value.toString());
			this.designOutputCard.command.changeElementLineColor(lineColor);
		}
		
		private function lineSizeChange(evt:SelectEvent):void{
			var lineSize:Number=new Number(this.lineSizeDrp.value.toString());
			this.designOutputCard.command.changeElementLineSize(lineSize);
		}
		private function lineAlphaChange(evt:SlideControlEvent):void{
			var val:Number=new Number(evt.val);
			if (!isNaN(val))
			{
				this.designOutputCard.command.changeElementLineAlpha(val * 0.01);
			}
		}
		
	}
}